package org.rainy.gift.service.impl;

import io.seata.spring.annotation.GlobalTransactional;
import org.rainy.gift.AuthClient;
import org.rainy.gift.domain.LoginUser;
import org.rainy.gift.domain.User;
import org.rainy.gift.domain.UserInfo;
import org.rainy.gift.dto.UserPhoneRegisterDTO;
import org.rainy.gift.exception.BusinessException;
import org.rainy.gift.exception.ResponseCode;
import org.rainy.gift.exception.RonghuaAssert;
import org.rainy.gift.mapper.UserMapper;
import org.rainy.gift.service.IUserService;
import org.rainy.gift.util.AjaxResult;
import org.rainy.gift.util.EmailUtil;
import org.rainy.gift.util.encrypt.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 * @author rainy
 * @since 2024-05-29
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private VerifyCodeServiceImpl verifyCodeService;
    @Autowired
    private UserInfoServiceImpl userInfoService;
    @Autowired
    private AuthClient authClient;
    @Autowired
    private EmailUtil emailUtil;


    /*
1.参数校验
1.1手机验证码校验是否匹配：将dto中的手机验证码与获取打验证码进行匹配
2.对密码进行加密加盐处理
3.添加数据到数据库：远程调用授权中心，保存数据到t_login_user,获取id，
如果远程调用失败则抛出异常注册失败，
如果成功,则要获取到loginUser的ID，根据获取到的login_id添加数据到t_user和t_user_info
4.发送短信和邮件通知用户注册成功*/
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void registerPhone(UserPhoneRegisterDTO userPhoneRegisterDTO) {
        String phoneByRedis = verifyCodeService.getSmsCode(userPhoneRegisterDTO.getPhone(), "user_register_sms_code_");
        RonghuaAssert.isEqIgnoreCase(phoneByRedis, userPhoneRegisterDTO.getVerifyCode(), ResponseCode.RESPONSE_CODE_10019);
        String salt = UUID.randomUUID().toString().replaceAll("-", "");
        String password = MD5.getMD5("rainy" + salt + userPhoneRegisterDTO.getPassword() + "rainy");
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername("rainy_" + UUID.randomUUID().toString().replaceAll("-", ""));
        loginUser.setSalt(salt);
        loginUser.setPassword(password);
        loginUser.setPhone(userPhoneRegisterDTO.getPhone());
        loginUser.setType(1);
        AjaxResult ajaxResult = authClient.addOrUpdate(loginUser);
        if (!ajaxResult.isSuccess()) {
            throw new BusinessException(ajaxResult.getMessage());
        }
        Long loginId = Long.parseLong(ajaxResult.getResultObj().toString());
        User user = new User();
        user.setCreateTime(new Date().getTime());
        user.setPhone(userPhoneRegisterDTO.getPhone());
        user.setSalt(salt);
        user.setPassword(password);
        user.setBitState(1L);
        user.setLoginId(loginId);
        insert(user);
        UserInfo userInfo = new UserInfo();
        userInfo.setCreateTime(new Date().getTime());
        userInfo.setRegTime(userInfo.getCreateTime());
        userInfo.setLevel(0);
        userInfo.setGrowScore(0);
        userInfo.setUserId(user.getId());
        userInfoService.insert(userInfo);
        System.out.println(1 / 0);
        emailUtil.sendSimpleEmail("1984289228@qq.com", "欢迎加入我们", "您已成功注册");
    }
}
